package gui.screens;

import gui.CanvasManager;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import application.MainController;
import domain.GameObject;

/**
 * The class for the shooting screen.
 * @author Sandra Mayer
 *
 */
public class ShootingScreen extends Canvas implements CommandListener {
	
	private static String FILENAME = "/spoted.jpg";
	private Command shootCmd;
	private Command cancelCmd;
	private Image rabbit;
	private GameObject rabbitObj;
	
	/**
	 * Constructor.
	 * @param gameObj the game object
	 */
	public ShootingScreen(GameObject gameObj) {
		try {
			shootCmd = new Command("Shoot", Command.CANCEL, 0);
			cancelCmd = new Command("Cancel", Command.EXIT, 0);
			
			rabbit = Image.createImage(FILENAME);
			rabbitObj = gameObj;

			this.addCommand(shootCmd);
			this.addCommand(cancelCmd);
			this.setCommandListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == shootCmd) {
			if (MainController.getInstance().use(rabbitObj)) {
				CanvasManager.setCurrent(new ShotScreen());
			} else {
				CanvasManager.showCanvas();
			}
		} else if (cmd == cancelCmd) {
			CanvasManager.showCanvas();
		}
		
	}

	/**
	 * Paints the screen.
	 * @param g Graphics
	 */
	protected void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - rabbit.getWidth()) / 2,
				(getHeight() - rabbit.getHeight()) / 2);

		g.drawImage(rabbit, 0, 0, Graphics.TOP | Graphics.LEFT);
	}
}
